package com.maimang.platform.dsp.controller;

import com.maimang.platform.annotation.Log;
import com.maimang.platform.base.BaseController;
import com.maimang.platform.base.Result;
import com.maimang.platform.common.utils.ExcelUtil;
import com.maimang.platform.dsp.entity.Advertiser;
import com.maimang.platform.dsp.entity.AdvertiserShort;
import com.maimang.platform.dsp.service.AdvertiserService;
import com.maimang.platform.enums.BusinessType;
import com.maimang.platform.lang.StringUtils;
import com.maimang.platform.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author Rocky
 * @version 1.0.0
 * @classname AdvertiserController
 * @description <br>广告主 信息操作处理</br>
 * @date 2019-07-31 10:07:36
 * @email jintaozhao@qq.com
 */
@Controller
@RequestMapping("/dsp/advertiser")
@Api(value = "广告主", description = "广告主 信息操作处理接口")
public class AdvertiserController extends BaseController {
	private String prefix = "dsp/advertiser";

	@Autowired
	private AdvertiserService advertiserService;

	@RequiresPermissions("dsp:advertiser:view")
	@GetMapping()
	public String advertiser() {
		return prefix + "/advertiser";
	}

	/**
	 * 查询广告主列表
	 */
	@ApiOperation(value = "查询列表", notes = "查询广告主列表信息")
	@RequiresPermissions("dsp:advertiser:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(Advertiser advertiser) {
		startPage();
		List<Advertiser> list = advertiserService.selectAdvertiserList(advertiser);
		return getDataTable(list);
	}


	/**
	 * 查询广告主列表
	 */
	@ApiOperation(value = "查询列表", notes = "查询广告主列表信息")
	@PostMapping("/getList")
	@ResponseBody
	public Result getList(String company) {
		if (StringUtils.isEmpty(company))
			return error("公司名称为空");

		Advertiser advertiser = new Advertiser();
		advertiser.setCompany(company);
		startPage();
		List<Advertiser> list = advertiserService.selectAdvertiserList(advertiser);
		return Result.SUCCESS(getDataTable(list));
	}

	/**
	 * 查询广告主列表
	 */
	@ApiOperation(value = "查询列表", notes = "查询广告主列表信息")
	@RequiresPermissions("dsp:advertiser:list")
	@PostMapping("/dataList")
	@ResponseBody
	public TableDataInfo dataList(Advertiser advertiser) {
		startPage();
		List<AdvertiserShort> list = advertiserService.selectShortList(advertiser);
		return getDataTable(list);
	}


	/**
	 * 导出广告主列表
	 */
	@ApiOperation(value = "导出列表", notes = "导出广告主列表信息到Excel文件")
	@RequiresPermissions("dsp:data:export")
	@Log(title = "广告主", businessType = BusinessType.EXPORT)
	@PostMapping("/export")
	@ResponseBody
	public Result export(Advertiser advertiser) {
		List<Advertiser> list = advertiserService.selectAdvertiserList(advertiser);
		ExcelUtil<Advertiser> util = new ExcelUtil<Advertiser>(Advertiser.class);
		return util.exportExcel(list, "广告主信息");
	}

	/**
	 * 新增广告主
	 */
	@GetMapping("/add")
	public String add() {
		return prefix + "/add";
	}

	/**
	 * 新增保存广告主
	 */
	@ApiOperation(value = "新增保存", notes = "参数：Advertiser")
	@RequiresPermissions("dsp:advertiser:add")
	@Log(title = "广告主", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public Result addSave(Advertiser advertiser) {
		return toResult(advertiserService.insertAdvertiser(advertiser));
	}

	/**
	 * 修改广告主
	 */
	@GetMapping("/edit/{id}")
	public String edit(@PathVariable("id") String id, ModelMap modelMap) {
		Long advertiserId = Long.parseLong(id);
		Advertiser advertiser = advertiserService.selectAdvertiserById(advertiserId);
		modelMap.put("advertiser", advertiser);
		return prefix + "/edit";
	}

	/**
	 * 修改保存广告主
	 */
	@ApiOperation(value = "修改保存", notes = "参数：Advertiser")
	@RequiresPermissions("dsp:advertiser:edit")
	@Log(title = "广告主", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public Result editSave(Advertiser advertiser) {
		return toResult(advertiserService.updateAdvertiser(advertiser));
	}

	/**
	 * 删除广告主
	 */
	@ApiOperation(value = "删除对象", notes = "参数：ids，示例： id1,id2,id3，接口会转转为String[]类型")
	@RequiresPermissions("dsp:advertiser:remove")
	@Log(title = "广告主", businessType = BusinessType.DELETE)
	@PostMapping("/remove")
	@ResponseBody
	public Result remove(String ids) {
		return toResult(advertiserService.deleteAdvertiserByIds(ids));
	}

}